package com.ajk.server.service.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ajk.server.dao.SuggestMapper;
import com.ajk.server.model.Suggest;
import com.ajk.server.model.Users;
import com.ajk.server.model.query.SuggestQuery;
import com.ajk.server.service.SuggestService;
import com.ajk.server.service.UsersService;

/**
 * 意见反馈  <br>
 * t_suggest <br>
 * v1.0 2016-05-01
 */
 @Service
 public class SuggestServiceImpl implements SuggestService {
	
	private static final Logger logger = LoggerFactory.getLogger(SuggestServiceImpl.class);
	
	@Autowired
	private SuggestMapper suggestMapper;
	
	@Autowired
	private UsersService usersService;
	
	/**
 	*根据ID删除 意见反馈
 	*/
 	@Override
 	public Integer deleteSuggestById(Integer id){
 		return suggestMapper.deleteById(id);
 	}
 	
 	/**
 	*新增 意见反馈
 	*/
 	@Override
 	@Transactional
 	public Integer batchInsertSuggest(List<? extends Suggest> suggests){
		Integer count = suggestMapper.batchInsert(suggests);
		return count;
 	}
 	/**
 	*新增 意见反馈
 	*/
 	@Override
 	@Transactional
 	public Integer insertSuggest(Suggest domain){
		Integer id = suggestMapper.insert(domain);
		return id;
 	}

 	/**
 	*更新 意见反馈
 	*/
 	@Override
 	@Transactional
 	public Integer updateSuggest(Suggest domain){
		Integer count = suggestMapper.update(domain);
		return count;
 	}
 	
 	/**
 	*根据ID查询 意见反馈
 	*/
 	public Suggest findSuggestById(Integer id){
 		Suggest result = suggestMapper.findById(id);
 		if(result!=null){
 			result.dealWith();
 		}
 		return result;
 	}

	/**
 	*通过query 查询 意见反馈
 	*/
	public List<Suggest> findSuggestByQuery(SuggestQuery query){
		query.dealWith();
		List<Suggest> result = suggestMapper.findByQuery(query);
		if(CollectionUtils.isNotEmpty(result)) {
			Set<Integer> userIds = new HashSet<>();
			for (Suggest suggest : result) {
				suggest.dealWith();
				userIds.add(suggest.getUserId());
			}
			
			Map<Integer,Users> map = usersService.findUsersMapByIds(new ArrayList<>(userIds));
			
			for (Suggest suggest : result) {
				Users users = map.get(suggest.getUserId());
				if(users!=null){
					suggest.setAvatar(users.getAvatar());
					suggest.setUserName(users.getName());
				}
			}
			
			Collections.sort(result,new Comparator<Suggest>() {
				@Override
				public int compare(Suggest o1, Suggest o2) {
					if(o1!=null && o2!=null && o1.getId()!=null && o2.getId()!=null){
						return o2.getId().compareTo(o1.getId());
					}
					return 0;
				}
			});
		}
		return result;
	}	
	
	/**
 	*通过query count总条数
 	*/
 	public Integer countSuggestByQuery(SuggestQuery query){
 		query.dealWith();
 		Integer result = suggestMapper.countByQuery(query);
 		return result;
 	}
 	
 	/**
 	* 通过ids查询
 	*/
 	public List<Suggest> findSuggestByIds(List<Integer> ids){
 		List<Suggest> result = suggestMapper.findByIds(ids);
		if(CollectionUtils.isNotEmpty(result)) {
			for (Suggest suggest : result) {
				suggest.dealWith();
			}
		}
		return result;
 	}
 	
}	